# coding:utf-8
# @Time    : 2022/01/11 10:00
# @Author  : Martin

from page_object.base.page_items import PageItems
from util.excel_write import write_excel


class PageItemsCoupon(PageItems):

    def input_rule_name(self, txt):
        """
        输入规则名称
        :param txt:
        :return:
        """
        self.input('id', 'ticketRuleName', txt)

    def input_rule_time(self, times: str):
        start_time, end_time = times.split(',')

        """
        输入活动时间
        :param txt:
        :return:
        """
        # # 输入开始时间
        # self.input('id', 'ticketStartDate', start_time)
        # iframe1 = self.locate('xpath', '//iframe[@hidefocus="true"]')
        # self.switch_frame(iframe1)
        #
        # # 点击确定
        # confirm = self.locate('css selector', '#dpOkInput')
        # self.excute_js("arguments[0].click();", confirm)
        # self.switch_default()
        #
        # # 输入结束时间
        # self.input('id', 'ticketEndDate', end_time)
        # iframe2 = self.locate('xpath', '//iframe[@hidefocus="true"]')
        # self.switch_frame(iframe2)
        #
        # # 点击确定
        # confirm = self.locate('css selector', '#dpOkInput')
        # self.excute_js("arguments[0].click();", confirm)
        # self.switch_default()

        self.js_input('#ticketStartDate', start_time)
        self.js_input('#ticketEndDate', end_time)


    def input_rule_show_info(self, cate=None, brand=None, single=None, custom=None):
        """
        输入规则展示信息
        :param custom:
        :param single:
        :param brand:
        :param cate:
        :return:
        """
        if cate and not brand:
            self.click('id', 'ticketRuleCateType')
            self.input('id', 'ticketRule-cateInfo', cate)
        elif cate and brand and not single:
            self.click('id ', 'ticketRuleBrandType')
            self.input('id', 'ticketRule-brandInfo1', cate)
            self.input('id', 'ticketRule-brandInfo2', brand)
        elif single:
            self.click('id', 'ticketRuleSingleType')
            self.input('id', 'ticketRule-singleInfo1', cate)
            self.input('id', 'ticketRule-singleInfo2', brand)
            self.input('id', 'ticketRule-singleInfo2', single)
        elif custom:
            self.click('id', 'ticketRuleCustomType')
            self.input('id', 'ticketRule-custom', custom)
        else:
            self.logger.warning('活动展示信息未输入，输入的维度信息不正确')

    def check_rule_channel(self, channel_ids):
        """
        勾选用券终端
        :param channel_ids:
        :return:
        """
        channel_ids = str(channel_ids).split(',')
        for channel in channel_ids:
            self.single_checked('xpath',
                                f'//input[@value="{channel}"][@name="ticketRule.ticketPosCode"]')

    def check_rule_store_type(self, store_type_ids):
        """
        选择业态类型
        :param store_type_ids:
        """
        store_type_ids = str(store_type_ids).split(',')
        for store_type in store_type_ids:
            self.single_checked('xpath', f'//input[@value="{store_type}"][@name="selectTicketStore"]')

        # 点击全选添加到列表
        self.click('id', 'addallst4')

    def select_rule_part_city(self):
        """
        点击部分城市
        :return:
        """
        self.click('id', 'cityTicketRadio')

    def select_rule_part_store(self):
        """
        点击部分门店
        :return:
        """
        self.click('id', 'ticketStoreRadio')

    def select_rule_province(self, province_ids):
        """
        选择省份
        :param province_ids:省份
        ('xpath', f'//input[@value="{province}"][@name="province"]')
        """
        province_ids = str(province_ids).split(',')
        for province in province_ids:
            self.scroll_single_checked('#st1', f'input[value="{province}"][name="provincet"]')
            # self.single_checked('xpath', f'//input[@value="{province}"][@name="province"]')

    def select_rule_area(self, area_ids):
        """
        选择省份
        :param area_ids:大区
        ('xpath', f'//input[@value="{province}"][@name="area"]')
        """
        # 勾选大区选项
        self.single_checked('xpath', '//input[@name="selectTicketArea"][@value="2"]')
        area_ids = str(area_ids).split(',')
        for area in area_ids:
            self.scroll_single_checked('#s1', f'input[value="{area}"][name="areat"]')

    def select_rule_city(self, city_ids):
        """
        选择城市
        :param city_ids:
        :return:
        """
        city_ids = str(city_ids).split(',')
        for city in city_ids:
            self.select('name', 'st2', city)
        self.click('id', 'addt')

    def select_rule_store(self, store_ids):
        """
        选择门店
        :param store_ids:
        :return:
        """
        # 选择门店
        # 点击全选，带出门店
        self.click('id', 'addallst')

        # 遍历选中门店
        store_ids = str(store_ids).split(',')
        for store in store_ids:
            self.select('name', 'sst2', store)

        # 点击添加，把已选中的门店添加到列表
        self.click('id', 'addsst')

    def check_rule_commodity_type(self, type_ids):
        """
        勾选规则参与互动商品范围类型
        :param type_ids:
        """
        type_ids = str(type_ids).split(',')
        for type_ in type_ids:
            self.single_checked('xpath', f'//input[@value="{type_}"][@name="ticketRule.commodityType"]')

    def input_coupon_price(self, balance):
        """
        输入券面值
        :param balance:
        :return:
        """
        self.input('name', 'freeCouponsRule.couponsPrice', balance)

    def select_coupon_type(self, coupon_type):
        """
        选择券类型
        :param coupon_type:
        :return:
        """
        self.select('name', 'freeCouponsRule.couponsType', coupon_type)

    def check_show_area(self, area_ids):
        """
        勾选展示区域
        :param area_ids:
        """
        for area in area_ids:
            # offLine_showArea
            self.single_checked('xpath', f'//input[@value="{area}"][@name="type6_showArea"]')

    def _select_rule_commdty_area_type(self, type_):
        """
        选择维护商品范围类型
        :param type_:
        singleType=单品添加
        cateType=目录添加
        brandType=品牌添加
        cateAndBrandType=目录品牌添加
        allCateType=添加全目录
        """
        self.single_checked('xpath', f'//input[@name="ticketCmmdAreaType_radio"][@id="{type_}"]')

    def _select_rule_relation(self, txt):
        """
        选择包含关系
        :param txt: 0：包含 1：排除
        :return:
        """
        self.select('id', 'ticketCmmdAreaType', txt, 'text')

    def _input_rule_cmmd(self, txt):
        self.input('id', 'ticketCmmdCode', txt)

    def _input_rule_cate(self, txt):
        self.input('id', 'ticketCmmdCate', txt)

    def _input_rule_brand(self, txt):
        self.input('id', 'ticketBrand', txt)

    def _input_rule_commdty_scope(self, code=None, cate=None, brand=None):
        if code:
            self._input_cmmd(code)
        if cate:
            self._input_cate(cate)
        if brand:
            self._input_brand(brand)

    def add_rule_single(self, params):
        """
        添加单品包含
        :param params:商品范围列表
        :return:
        """
        self._select_rule_relation('包含')
        params = str(params).split(',')
        for param in params:
            self._input_rule_cmmd(param)
            self.click('id', 'ticketCmmdCodeAdd')
        self.click('link text', '添加到商品范围')

    def add_rule_single_exclude(self, txt):
        """
        添加单品排除
        :param txt:
        :return:
        """
        self._select_rule_relation('排除')
        codes = str(txt).split(',')
        for code in codes:
            self._input_rule_cmmd(code)
            self.click('id', 'ticketCmmdCodeAdd')
        self.click('link text', '添加到商品范围')

    def add_rule_cate(self, txt):
        """
        添加目录包含
        :param txt:
        :return:
        """
        self.click('id', 'ticketCateType')
        self._select_rule_relation('包含')
        codes = str(txt).split(',')
        for code in codes:
            self._input_rule_cate(code)
            self.click('id', 'ticketCmmdCateAdd')
        self.click('link text', '添加到商品范围')

    def add_rule_cate_exclude(self, txt):
        """
        添加目录排除
        :param txt:
        :return:
        """
        self.click('id', 'ticketCateType')
        self._select_rule_relation('排除')
        codes = str(txt).split(',')
        for code in codes:
            self._input_rule_cate(code)
            self.click('id', 'ticketCmmdCateAdd')
        self.click('link text', '添加到商品范围')

    def add_rule_brand(self, txt):
        """
        添加品牌包含
        :param txt:
        :return:
        """
        self.click('id', 'ticketBrandType')
        self._select_rule_relation('包含')
        codes = str(txt).split(',')
        for code in codes:
            self._input_rule_brand(code)
            self.click('xpath', '//a[@onclick="ticketBrandAdd()"]')
        self.click('link text', '添加到商品范围')

    def add_rule_brand_exclude(self, txt):
        """
        添加品牌排除
        :param txt:
        :return:
        """
        self.click('id', 'ticketBrandType')
        self._select_rule_relation('排除')
        codes = str(txt).split(',')
        for code in codes:
            self._input_rule_brand(code)
            self.click('xpath', '//a[@onclick="ticketBrandAdd()"]')
        self.click('link text', '添加到商品范围')

    def add_rule_cate_brand(self, txt):
        """
        添加目录品牌包含
        :param txt:
        :return:
        """
        self.click('id', 'ticketCateAndBrandType')
        self._select_rule_relation('包含')
        codes = str(txt).split(',')
        for code in codes:
            cate, brand = code.split('_')
            self._input_rule_cate(cate)
            self.click('id', 'ticketCmmdCateAdd')
            self._input_rule_brand(brand)
            self.click('xpath', '//a[@onclick="ticketBrandAdd()"]')
            self.click('link text', '添加到商品范围')

    def add_rule_cate_brand_exclude(self, txt):
        """
        添加目录品牌排除
        :param txt:
        :return:
        """
        self.click('id', 'ticketCateAndBrandType')
        self._select_rule_relation('排除')
        codes = str(txt).split(',')
        for code in codes:
            cate, brand = code.split('_')
            self._input_rule_cate(cate)
            self.click('id', 'ticketCmmdCateAdd')
            self._input_rule_brand(brand)
            self.click('xpath', '//a[@onclick="ticketBrandAdd()"]')
            self.click('link text', '添加到商品范围')

    def import_rule_act_scope(self, params):
        """
        批量导入商品范围
        :param params:
        :return:
        """
        index = 0
        for param in params:
            params[index] = param.split('_')
            index += 1

        path = '/excel_template/ticketScopeTemplate.xls'
        write_excel(path, params)

        self.click('id', 'batch')
        # 确认切换维护方式 '商品范围维护方式切换时会删除已添加的商品范围！'
        self.switch_alert()
        self.upload('id', 'ticketSingleFile', path)
        self.click('xpath', '//a[@onclick="importTicketScope();"]')
        # 确认导入结果 '本次导入数据2条数据，成功2条，重复0条！'
        return self.switch_alert()

    def input_rule_limit(self, args: list):
        table = self.locate('xpath', '//h1[text()="券优惠力度"]')
        self.scroll_ele(table)
        for arg in args:
            cal_type, cal_limit, amount = arg.split(',')

            index = args.index(arg) + 1
            # 点击增加
            self.logger.info('点击增加用券规则')
            self.click('xpath', '//a[contains(@onclick,"addTicketRule();")]')
            # 输入门槛类型
            self.logger.info('输入门槛类型')
            self.select('css selector', f'#detailinfoRule>.uiTable1.detailedtableRule:nth-child({index})'
                                        ' select[name="ticketRuleSpecial.calculationMode"]', cal_type, type_="text")
            # 输入门槛金额
            self.logger.info('输入门槛金额')
            self.input('css selector', f'#detailinfoRule>.uiTable1.detailedtableRule:nth-child({index})'
                                       ' input[name="ticketRuleSpecial.privilegeSill"]', cal_limit)
            # 输入优惠金额
            self.logger.info('输入优惠金额')
            self.input('css selector', f'#detailinfoRule>.uiTable1.detailedtableRule:nth-child({index})'
                                       ' input[name="ticketRuleSpecial.privilegeMode"]', amount)

    def input_use_scale(self, txt):
        self.input('name', 'noshoplimitcondition.usePercent', txt)

if __name__ == '__main__':
    # dd = {'com': 1, 'bu': 2, 'brand': 2, 'store': 2}
    # cc = list(dd.keys())
    # cc.sort()
    # print(cc)
    # print(list(dd.keys())==['a', 'b'])
    # print(PageItems.cal_after_amount('0.11', '100.23', '100.00'))
    print(float(0.1))
